package com.masterclient.app.modules.goods.contract;

import android.text.TextUtils;

import com.eoner.common.config.Config;
import com.eoner.common.bean.base.CommonBaseBean;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselibrary.bean.goods.AddFavoriteBean;
import com.eoner.baselibrary.bean.goods.GoodDetailBean;
import com.eoner.baselibrary.bean.goods.GoodsCommentBean;
import com.eoner.baselibrary.bean.goods.ProductTeamBean;
import com.eoner.baselibrary.bean.goods.ProductTeamViewBean;
import com.masterclient.app.modules.order.beans.ConfirmCartBean;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2017/06/29
*/
public class GoodsDetailPresenter extends GoodsDetailContract.Presenter{

    @Override
    public void getGoodsDetail(String id) {
        long timestamp = System.currentTimeMillis()/1000;
        Observable<GoodDetailBean> observable = RetrofitUtil.getInstance().getGoodsDetail(NetEnvManager.BASE_SURL+"1/product/detail/id_"+id+".html");
        Disposable disposable = toSubscribe(observable, new Consumer<GoodDetailBean>() {
            @Override
            public void accept(@NonNull GoodDetailBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showGoodDetail(bean.getData());
                }else {
                    mView.showMsg(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void addFavorite(@NonNull String customer_id,@android.support.annotation.NonNull String id, @android.support.annotation.NonNull String type) {

        long timestamp = System.currentTimeMillis()/1000;
        Observable<CommonBaseBean> observable = RetrofitUtil.getInstance().addFavorite(String.valueOf(timestamp),id,type);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showCollectSuccess();
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
            }
        });
        mRxManager.add(disposable);

    }


    @Override
    public void removeFavorite(@NonNull String customer_id,@android.support.annotation.NonNull String id, @android.support.annotation.NonNull String type) {
        long timestamp = System.currentTimeMillis()/1000;
        Observable<CommonBaseBean> observable = RetrofitUtil.getInstance().removeFavorite(String.valueOf(timestamp),id,type);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showRemoveCollectSuccess();
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void addToCart(@android.support.annotation.NonNull String product_id, @android.support.annotation.NonNull String qty,@android.support.annotation.NonNull String customer_id) {
        long timestamp = System.currentTimeMillis()/1000;

        Observable<CommonBaseBean> observable = RetrofitUtil.getInstance().addToCart(product_id,qty,String.valueOf(timestamp));
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    if(!TextUtils.isEmpty(bean.getArgs().cartNum+"")){
                        Config.CART_NUM = Integer.valueOf(bean.getArgs().cartNum);
                    }

                    mView.addCartSuccess();
                }else {
                    mView.showMsg(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void buyRightNow(String address_id, String product_id, String buy_num,String is_group_buy,String is_original_price,String team_id) {
        Map<String,String> map = new HashMap<>();
        if(!TextUtils.isEmpty(address_id)){
            map.put("address_id",address_id);
        }
        map.put("buy_product["+product_id+"]",buy_num);

        if(!TextUtils.isEmpty(is_group_buy)){
            map.put("is_group_buy",is_group_buy);
        }
        if(!TextUtils.isEmpty(is_original_price)){
            map.put("is_original_price",is_original_price);
        }
        if(!TextUtils.isEmpty(team_id)){
            map.put("team_id",team_id);
        }
        Observable<ConfirmCartBean> observable = RetrofitUtil.getInstance().confirmOrder(map,new String[]{});
        Disposable disposable = toSubscribe(observable, new Consumer<ConfirmCartBean>() {
            @Override
            public void accept(@NonNull ConfirmCartBean bean) throws Exception {
                if(bean!=null && bean.getData()!=null){
                    mView.confirmCartSuccess(bean.getData());
                }else {
                    mView.buyRightNowSuccess(bean.getMsg());
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void getCommentList(String is_image, String page, String page_size, String product_id) {
        Observable<GoodsCommentBean> observable = RetrofitUtil.getInstance().getCommentGoodsList(NetEnvManager.BASE_SURL+"1/product/get_product_review/product_id_"+product_id+"/"+page+"-"+page_size+".html");

        Disposable disposable = toSubscribe(observable, new Consumer<GoodsCommentBean>() {
            @Override
            public void accept(@NonNull GoodsCommentBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showGoodsComment(bean.getData());
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void addFavorite(String product_id) {
        Observable<AddFavoriteBean> observable = RetrofitUtil.getInstance().addFavorite(product_id);
        Disposable disposable = toSubscribe(observable, new Consumer<AddFavoriteBean>() {
            @Override
            public void accept(@NonNull AddFavoriteBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                  mView.addFavoriteSuccess(bean.getData().getSh_type());
                }else {
                  mView.addFavoriteFailed();
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void addShareInfo(String product_id) {
        Observable<CommonBaseBean> observable = RetrofitUtil.getInstance().addShareInfo(product_id);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
//                    mView.addFavoriteSuccess(bean.getData().getSh_type());
                    mView.addShareSuccess();
                }else {
//                    mView.addFavoriteFailed();
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void productTeam(String id, String page, String page_size) {
        Observable<ProductTeamBean> observable = RetrofitUtil.getInstance().productTeam(NetEnvManager.BASE_SURL+"1/group_buy/product_team/id_"+id+"/"+page+"_"+page_size+".html");
        Disposable disposable = toSubscribe(observable, new Consumer<ProductTeamBean>() {
            @Override
            public void accept(@NonNull ProductTeamBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showTeamList(bean.getData());
                }else {
                    mView.showTeamFailed();
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void productTeamView(String id) {
        Observable<ProductTeamViewBean> observable = RetrofitUtil.getInstance().productTeamView(NetEnvManager.BASE_SURL+"1/group_buy/product_team_view/id_"+id+".html");
        Disposable disposable = toSubscribe(observable, new Consumer<ProductTeamViewBean>() {
            @Override
            public void accept(@NonNull ProductTeamViewBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showProductViewBean(bean.getData());
                }else {
                    mView.showProductViewBeanFail();
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void pickUpSalesrule(String coupon_code) {
        Observable<CommonBaseResponse<Map<String,String>>> observable = RetrofitUtil.getInstance().pickUpSalesrule(coupon_code);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseResponse<Map<String,String>>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<Map<String,String>> bean) throws Exception {
                if(bean.getCode()==20002){
//                    mView.addFavoriteSuccess(bean.getData().getSh_type());
                    mView.pickUpSalesruleSuccess(coupon_code,bean.getMsg(),bean.getData());
                }else if (bean.getCode()==0){
                    mView.pickUpSalesruleSuccess(coupon_code,"领取成功",null);
                }else {
//                    mView.addFavoriteFailed();
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }
}